﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaygunDiplomacy.Graphics;
using System.Windows;
using System.Drawing;
using SlimDX.Direct3D9;
using SlimDX;

namespace RaygunDiplomacy.SpritesModule
{
   public class SpriteRenderer : IRenderer
   {
      #region Fields

      ObjectStateToSpriteState stateQuery;

      #endregion

      #region Properties

      public ObjectStateToSpriteState StateQuery
      {
         get { return stateQuery; }
         set { stateQuery = value; }
      }

      public Color4 Color { get; set; }

      public Vector3 WorldPosition { get; set; }

      public Vector3 ScreenPosition { get; set; }

      public Vector3 FrameCenter { get; set; }

      public Rectangle FrameRectangle { get; set; }

      public Texture CurrentTexture { get; set; }

      public bool IsVisible { get; set; }

      public Layers Layer { get; set; }

      #endregion

      #region Methods

      public void Update(TimeSpan elapsed, DateTime frameNow)
      {
         int currentAnimation = 0;

         float animationPoint = 0;

         if (stateQuery != null)
         {
            if (!stateQuery.IsVisible)
            {
               IsVisible = false;
               return;
            }
            else
            {
               IsVisible = true;
            }
            currentAnimation = stateQuery.CurrentAnimation;

            WorldPosition = stateQuery.PositionWorld;

            if (stateQuery.UseAnimationPoint)
            {
               animationPoint = stateQuery.AnimationPoint;
            }
            else
            {
               animationPoint = CalculateAnimationPoint(elapsed, frameNow);
            }
         }
         else
         {
            animationPoint = CalculateAnimationPoint(elapsed, frameNow);
         }

         CurrentTexture = GetAnimationTexture(currentAnimation);

         int currentFrame = GetCurrentFrame(currentAnimation, animationPoint);

         FrameRectangle = GetFrameRectangle(currentAnimation, currentFrame);

         FrameCenter = GetFrameCenter(currentAnimation, currentFrame);
      }

      private Rectangle GetFrameRectangle(int currentAnimation, int currentFrame)
      {
         return new Rectangle(0, 0, 1, 1);
      }

      private Vector3 GetFrameCenter(int currentAnimation, int currentFrame)
      {
         return new SlimDX.Vector3(0, 0, 0);
      }

      private int GetCurrentFrame(int currentAnimation, float animationPoint)
      {
         return 0;
      }

      private Texture GetAnimationTexture(int currentAnimation)
      {
         return null;
      }

      private float CalculateAnimationPoint(TimeSpan elapsed, DateTime frameNow)
      {
         return 0;
      }

      public bool CheckVisibility(Camera camera)
      {
         if (this.IsVisible)
         {
            int minX = 0 - (int)this.FrameCenter.X;
            int minY = 0 - (int)this.FrameCenter.Y;

            int maxX = camera.ViewWidth + (int)this.FrameRectangle.Width - (int)this.FrameCenter.X;
            int maxY = camera.ViewHeight + (int)this.FrameRectangle.Height - (int)this.FrameCenter.Y;

            if (this.WorldPosition.X >= minX && this.WorldPosition.X < maxX &&
               this.WorldPosition.Y >= minY && this.WorldPosition.Y < maxY)
            {
               this.ScreenPosition = this.WorldPosition - camera.LookAt;
               return true;
            }
         }

         return false;
      }

      public void Render(Camera camera)
      {
         SpriteDrawer.QueueDrawing(this.CurrentTexture, this.FrameRectangle, this.FrameCenter, this.ScreenPosition, this.Color);
      }

      public void Dispose()
      {
         stateQuery = null;
      }

      #endregion
   }
}
